package controllers.userC

import dao.{AccountDao, DataDao, ModeDao, UserDao}
import javax.inject.Inject
import play.api.libs.json.Json
import play.api.mvc.{AbstractController, ControllerComponents}
import tool.{FormTool, Tool}
import utils.Utils

import scala.concurrent.ExecutionContext.Implicits.global

/**
 * Created by yz on 21/7/2020
 */
class DataController @Inject()(cc: ControllerComponents)(
  implicit val userDao: UserDao,
  implicit val accountDao: AccountDao,
  implicit val modeDao: ModeDao,
  implicit val dataDao: DataDao
) extends AbstractController(cc) {

  def viewBefore = Action { implicit request =>
    Ok(views.html.user.data.view())
  }

  def detailInfoBefore = Action { implicit request =>
    val data = FormTool.barcodeForm.bindFromRequest().get
    Ok(views.html.user.data.detailInfo(data.barcode))
  }

  def getDataByBarcode = Action.async { implicit request =>
    val data = FormTool.barcodeForm.bindFromRequest().get
    dataDao.select(data.barcode).map { x =>
      val json = Utils.getJsonByT(x)
      Ok(Json.toJson(json))
    }
  }

  def getAllDataByBarCode = Action.async { implicit request =>
    val data = FormTool.barcodeForm.bindFromRequest().get
    dataDao.selectAll(data.barcode).map { rows =>
      val array = Tool.getArrayByDatas(rows.toList)
      val json = Json.toJson(array)
      Ok(json)
    }
  }

  def getAllDataByBarcodeAndRefGene = Action.async { implicit request =>
    val data = FormTool.barcodeForm.bindFromRequest().get
    val refGene = FormTool.refGeneForm.bindFromRequest().get.refGene
    dataDao.selectAll(data.barcode,refGene).map { rows =>
      val array = Tool.getArrayByDatas(rows.toList)
      val json = Json.toJson(array)
      Ok(json)
    }
  }

  def getAllRefGeneByBarcode = Action.async { implicit request =>
    val data = FormTool.barcodeForm.bindFromRequest().get
    dataDao.selectAllRefGene(data.barcode).map { xs =>
      val json = Json.toJson(xs)
      Ok(json)
    }
  }

  def searchBefore = Action { implicit request =>
    Ok(views.html.user.data.search())
  }

  def getAllBarcode = Action.async { implicit request =>
    dataDao.selectSingleColumn("barcode").map { xs =>
      val json = Json.toJson(xs)
      Ok(json)
    }
  }

  def getAllSampleName = Action.async { implicit request =>
    dataDao.selectSingleColumn("sampleName").map { xs =>
      val json = Json.toJson(xs)
      Ok(json)
    }
  }

  def getAllDataBySampleName = Action.async { implicit request =>
    val data = FormTool.sampleNameForm.bindFromRequest().get
    dataDao.selectAllBySampleName(data.sampleName).map { rows =>
      val array = Tool.getArrayByDatas(rows.toList)
      val json = Json.toJson(array)
      Ok(json)
    }
  }

  def getSingleColumnAll = Action.async { implicit request =>
    val data=FormTool.columnNameForm.bindFromRequest().get
    dataDao.selectSingleColumn(data.columnName).map { xs =>
      val json = Json.toJson(xs)
      Ok(json)
    }
  }

  def getAllDataByRefGene = Action.async { implicit request =>
    val data = FormTool.refGeneForm.bindFromRequest().get
    dataDao.selectAllByRefGene(data.refGene).map { rows =>
      val array = Tool.getArrayByDatas(rows.toList)
      val json = Json.toJson(array)
      Ok(json)
    }
  }

}
